
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :Wing
	CreateTime:2008-02-04 10:10:46
*/
public class UserFuncSrv extends IHandler
{

	public UserFuncSrv() 
	{
		super();
	}
	private void deleteUserFunc(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		UserFunc obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = UserFunc.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveUserFunc(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		UserFunc obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = UserFunc.fetch();
    	}
    	else                     //update
    	{
    		obj = UserFunc.fetch(con,id);
    	}
		UserFuncPageData data = (UserFuncPageData)PageData.fromForm(context,UserFuncPageData.class);
		context.setValue("UserFuncPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listUserFunc(DBConnection con,SrvContext context,String path) throws Exception
	{
		UserFuncSearchEvent event = (UserFuncSearchEvent)SearchEvent.fromForm(context,UserFuncSearchEvent.class);
		context.setSessionValue("UserFuncSearchEvent",event);
		context.setNextUrl(path+"UserFuncList.jsp");
	}
	private void backUserFunc(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"UserFuncList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listUserFunc"))
		{
			listUserFunc(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewUserFunc"))
		{
			String id = context.getString("Id",null);
			UserFunc obj = UserFunc.fetch(con,id);
			context.setValue("UserFunc",obj);
			context.setNextUrl(path+"UserFuncView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteUserFunc"))
		{
			context.setErrorUrl(path+"UserFuncList.jsp");
			deleteUserFunc(con,context);
			backUserFunc(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddUserFunc"))
		{
			UserFunc obj = UserFunc.fetch();
			UserFuncPageData data = (UserFuncPageData)PageData.fromTable(obj,UserFuncPageData.class);
			context.setValue("UserFuncPageData",data);
			context.setNextUrl(path+"UserFuncEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateUserFunc"))
		{
		    String id = context.getString("userId",null);
            if(id != null){
                User objUser = User.fetch(con,id);
                context.setSessionValue("UserDetail",objUser);
                Menu objMenu = Menu.fetch();
                context.setSessionValue("MenuDetail",objMenu);
                Function objFunc = Function.fetch();
                context.setSessionValue("FuncDetail",objFunc);
            }
			UserFunc obj = UserFunc.fetch(con,id);
			UserFuncPageData data = (UserFuncPageData)PageData.fromTable(obj,UserFuncPageData.class);
			context.setValue("UserFuncPageData",data);
			context.setNextUrl(path+"UserFuncEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyUserFunc"))
		{
			String id = context.getString("Id",null);
			UserFunc obj = UserFunc.copy(UserFunc.fetch(con,id));
			UserFuncPageData data = (UserFuncPageData)PageData.fromTable(obj,UserFuncPageData.class);
			context.setValue("UserFuncPageData",data);
			context.setNextUrl(path+"UserFuncEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveUserFunc"))
		{
			context.setErrorUrl(path+"UserFuncEdit.jsp");
			saveUserFunc(con,context,loginContext);
			backUserFunc(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewUserFunc"))
		{
			context.setErrorUrl(path+"UserFuncEdit.jsp");
			saveUserFunc(con,context,loginContext);
			UserFunc obj = UserFunc.fetch();
			UserFuncPageData data = (UserFuncPageData)PageData.fromTable(obj,UserFuncPageData.class);
			context.setValue("UserFuncPageData",data);
			context.setNextUrl(path+"UserFuncEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListUserFunc"))
		{
			backUserFunc(con,context,path);
			return;
		}
		
		//add by Wing start

		//add by Wing end

		Logger.throwException("UserFuncSrv","unknown action_code="+action_code);
	}
}
